package com.example.delll.kugou.activity;

import android.nfc.Tag;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.example.delll.kugou.R;
import com.example.delll.kugou.fragment.JJFragment;
import com.example.delll.kugou.fragment.MyFragment;
import com.example.delll.kugou.fragment.SPFragment;
import com.example.delll.kugou.fragment.SYFragment;


public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private TabLayout tabLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intoView();
        intoTabLayout();
        showFragment(0);
    }

    /**
     * 初始化 TabLayout 标题 并 设置监听事件
     */
    private void intoTabLayout() {
        tabLayout.addTab(tabLayout.newTab().setText("首页").setTag(0));
        tabLayout.addTab(tabLayout.newTab().setText("视频").setTag(1));
        tabLayout.addTab(tabLayout.newTab().setText("讲讲").setTag(2));
        tabLayout.addTab(tabLayout.newTab().setText("我").setTag(3));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int tag = (int) tab.getTag();
                showFragment(tag);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private int lastIndex=-1;
    private Fragment[] fragments = new Fragment[4];

    /**
     * 碎片管理器
     * @param nextIndex 当前 TabLayout Tag 对应的碎片
     */
        private void showFragment(int nextIndex) {
            if (nextIndex == lastIndex) {
                return;
            }
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            if (fragments[nextIndex] == null) {
                switch (nextIndex) {
                    case 0:
                        fragments[nextIndex] = new SYFragment();
                        break;
                    case 1:
                        fragments[nextIndex] = new SPFragment();
                        break;
                    case 2:
                        fragments[nextIndex] = new JJFragment();
                        break;
                    case 3:
                        fragments[nextIndex] = new MyFragment();
                        break;
                }
                ft.add(R.id.main_fl, fragments[nextIndex]);
            } else {
                ft.show(fragments[nextIndex]);
            }
            if (lastIndex != -1) {
                ft.hide(fragments[lastIndex]);
            }
            ft.commit();
            lastIndex=nextIndex;
        }

    /**
     * 初始化View
     */
    private void intoView() {
        tabLayout = findViewById(R.id.tab_layout);
    }


}
